﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace OpenShape
{
    public partial class Cluster : Form
    {
        int Index;
        string IdxStr = "";
        string AnalyStr = ""; // 군집분석을 위한 인덱스 저장스트링
        DataTable dt = new DataTable();
        StringBuilder sb = new StringBuilder();
        charF pchar = new charF();

       
        public Cluster(int isID)
        {
            InitializeComponent();
            Index = isID;
            dt = LayerManager.m_Layers[Index].m_DBF.pDataTable;
            sb.Remove(0, sb.Length);
            //txtLabelField.Enabled = false;
        }

        private void Cluster_Load(object sender, EventArgs e)
        {
            ReadCol();
        }


        #region == 초기화  ==
        private void ReadCol()
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                lstFieldList.Items.Add(dt.Columns[i].ColumnName.ToString());

                string str_temp = "";
                for (int k = i.ToString().Length; k < 3; k++)
                {
                    str_temp += "o";
                }
                str_temp = str_temp + i.ToString();

                lstbox1.Items.Add(str_temp);
            }
        }
        #endregion

        #region == 리스트 추가 ==
        private void button1_Click(object sender, EventArgs e)
        {
            int k = lstFieldList.SelectedIndex;

            lstSelectField.Items.Add(lstFieldList.SelectedItem);
            lstbox2.Items.Add(lstbox1.Items[k].ToString());

            IdxStr = IdxStr + lstbox1.Items[k].ToString() + "/" ;
            txt1.Text = IdxStr;
            
            lstFieldList.Items.Remove(lstFieldList.SelectedItem);
            lstbox1.Items.Remove(lstbox1.Items[k].ToString());
        }
        #endregion


        #region == 리스트 삭제 ==
        private void button6_Click(object sender, EventArgs e)
        {
            int k = lstSelectField.SelectedIndex;

            lstFieldList.Items.Add(lstSelectField.SelectedItem);
            lstbox1.Items.Add(lstbox2.Items[k].ToString());
            
            IdxStr = txt1.Text;
            txt1.Text =  pchar.Erase(IdxStr, k * 4, 4);
           

            lstSelectField.Items.Remove(lstSelectField.SelectedItem);
            lstbox2.Items.Remove(lstbox2.Items[k].ToString());
        }
        #endregion

        #region == 라벨 추가 ==
        private void button2_Click(object sender, EventArgs e)
        {
            int k = lstFieldList.SelectedIndex;

            //txtLabelField.Enabled = true;
            label1.Text = lstFieldList.SelectedItem.ToString();
            //txtLabelField.Enabled = false;
            
            txt2.Text = lstbox1.Items[k].ToString();
            

            lstFieldList.Items.Remove(lstFieldList.SelectedItem);
            lstbox1.Items.Remove(lstbox1.Items[k].ToString());
        }
        #endregion

       
        #region == 라벨 삭제 ==
        private void button7_Click(object sender, EventArgs e)
        {
            lstFieldList.Items.Add(label1.Text);
            lstbox1.Items.Add(txt2.Text);

            //txtLabelField.Enabled = true;
            label1.Text = "";
            //txtLabelField.Enabled = false;

            txt2.Text = "";
        }
         #endregion

        private void lstSelectField_MouseClick(object sender, MouseEventArgs e)
        {
            button6.Visible = true;
            button1.Visible = false;
        }

        private void lstFieldList_MouseClick(object sender, MouseEventArgs e)
        {
            button1.Visible = true;
            button2.Visible = true;
            button6.Visible = false;
            button7.Visible = false;
        }

        private void txtLabelField_MouseClick(object sender, MouseEventArgs e)
        {
            button7.Visible = true;
            button2.Visible = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            GISViewer gForm = new GISViewer();
            gForm.Focus();
            this.Dispose();
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            lstbox1.Items.Clear();
            lstbox2.Items.Clear();
            lstFieldList.Items.Clear();
            lstSelectField.Items.Clear();
            txtLabelField.Text = "";
            label1.Text = "";
            txt1.Text = "";
            txt2.Text = "";

            ReadCol();

        }


        private void Unload()
        {
            this.Dispose();
        }

        #region == 확인 버튼 Cluster -> ClusterAnaly ==
        private void button3_Click(object sender, EventArgs e)
        {
            // ClusterAnaly로 넘길 값 정의/정리
            AnalyStr = "";
            AnalyStr = txt1.Text + txt2.Text;
            CStatics.AnalyDBF = AnalyStr;

            ClusterAnaly CA = new ClusterAnaly(dt);
            CA.Show();
            CA.Focus();
            Unload();
            
        }
        #endregion





    }
}
